package com.inspur.resourcequery.fragment;


import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;

import com.inspur.resourcequery.PhysicsInfoActivity;
import com.inspur.resourcequery.R;
import com.inspur.resourcequery.ResDetailActivity;
import com.inspur.resourcequery.adapter.PhysicsListAdapter;
import com.inspur.resourcequery.adapter.PhysicsPortAdapter;
import com.inspur.resourcequery.model.PhysicsModel;
import com.inspur.resourcequery.model.ResData;

public class PhysicsPortFragment extends Fragment{

	private PhysicsModel physicsModel;
	private List<ResData> list;
	private PhysicsPortAdapter adapter;
	private TextView nodataTv;
	private GridView grid;;
	private Context mContext;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_port, null);	
		mContext = inflater.getContext();
		PhysicsInfoActivity activity = (PhysicsInfoActivity) inflater.getContext();
		physicsModel = activity.getPhysicsModel();
		list = physicsModel.getPortList();
		
		nodataTv = (TextView)view.findViewById(R.id.tv_nodata);
		grid = (GridView)view.findViewById(R.id.gv_port);
		grid.setOnItemClickListener(itemClickListener);
		
		if(list.size() == 0){
			nodataTv.setText("没有发现端口数据！");
			nodataTv.setVisibility(0);
			grid.setBackgroundColor(Color.parseColor("#00000000"));
		} else {
			adapter = new PhysicsPortAdapter(activity, list);
			grid.setAdapter(adapter);
		}
		return view;
	}
	
	/**
	 * 列表数据项点击监听事件，转向资源详情页面
	 */
	OnItemClickListener itemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Intent intent = new Intent(mContext, ResDetailActivity.class);
			Bundle bundle = new Bundle();
			bundle.putSerializable("data", list.get(position));
			intent.putExtras(bundle);
			startActivity(intent);
		}
	};
}
